package view;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import controler.SistemaIndumentaria;

public class AgregarMaterialYCantidad extends JFrame {
	private static final long serialVersionUID = 1L;
	private JLabel lblCodigoPrenda;
	private JTextField txtCodigoPrenda;
	private JLabel lblCodigoMaterial;
	private JTextField txtCodigoMaterial;
	private JLabel lblCantidad;
	private JTextField txtCantidad;
	private JButton btnAgregar;
	private JButton btnCerrar;
	private Container contenedor;
	
	
	public AgregarMaterialYCantidad(int idPrenda){

		
		this.setVisible(true);
		setBounds(100, 100, 450, 300);
		this.setLocationRelativeTo(null);
		this.setTitle("Agregar material a prenda");
		this.setLayout(new GridLayout(8,2));
		
		Container contenedor = this.getContentPane();
		

		lblCodigoPrenda = new JLabel();
		lblCodigoPrenda.setText("Codigo de Prenda:");
		contenedor.add(lblCodigoPrenda);
		
		txtCodigoPrenda = new JTextField();
		txtCodigoPrenda.setSize(30,100);
		txtCodigoPrenda.setText(String.valueOf(idPrenda));
		contenedor.add(txtCodigoPrenda);
		
		lblCodigoMaterial = new JLabel();
		lblCodigoMaterial.setText("Codigo de Material:");
		contenedor.add(lblCodigoMaterial);
		
		txtCodigoMaterial = new JTextField();
		txtCodigoMaterial.setSize(30,100);
		contenedor.add(txtCodigoMaterial);
		
		lblCantidad = new JLabel();
		lblCantidad.setText("Cantidad:");
		contenedor.add(lblCantidad);
		
		txtCantidad = new JTextField();
		txtCantidad.setSize(30,100);
		contenedor.add(txtCantidad);
		
		btnAgregar = new JButton();
		btnAgregar.setText("Agregar");
		btnAgregar.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				if((!txtCodigoMaterial.getText().equalsIgnoreCase(""))&&(!txtCodigoPrenda.getText().equalsIgnoreCase(""))&&(!txtCantidad.getText().equalsIgnoreCase(""))){

					if (SistemaIndumentaria.getInstancia().agregarMaterialAPrenda(Integer.parseInt(txtCodigoMaterial.getText()), Integer.parseInt(txtCodigoPrenda.getText()),  Integer.parseInt(txtCantidad.getText())))
					{				
						txtCodigoMaterial.setText("");
						txtCantidad.setText("");
					}
					else{	
						JOptionPane.showMessageDialog(AgregarMaterialYCantidad.this.contenedor,"El material no se agrego",
							    "material no existente",
							    JOptionPane.ERROR_MESSAGE); 
					}
				}
				else{
					JOptionPane.showMessageDialog(AgregarMaterialYCantidad.this.contenedor,"Todos los campos son requeridos",
						    "Campos vacios",
						    JOptionPane.ERROR_MESSAGE); 	
				}
			}
		});
		contenedor.add(btnAgregar);
		btnCerrar = new JButton();
		btnCerrar.setText("Cerrar");
		contenedor.add(btnCerrar);	
		btnCerrar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				AgregarMaterialYCantidad.this.setVisible(false);
			}
		});
		
	}
	}
	

